﻿using UnityEngine;
using UnityEngine.UI;
using shaco.ActionExtensions;
using System.Collections;
using System;
using shaco.Base;
using YooAsset;

[shaco.UILayerOpenAsync]
public class MainLayer : UnityEngine.MonoBehaviour, shaco.IUIAnimation
{
    public class TestAudio : shaco.Base.IAudio, shaco.IAudioWillCreate, shaco.IAudioWillPlay, shaco.IAudioWillResume, shaco.IAudioWillPause, shaco.IAudioWillStop, shaco.IAudioWillAutoStop
    {
        public override string path => _path;
        string _path = "";

        public TestAudio(string v) => _path = v;

        public void OnWillCreate(string path, Action<IResourceHandle> callbackCreate)
        {
            shaco.Log.Info($"TestAudio OnWillCreate: {_path}");
            var request = shaco.GameHelper.res.LoadWithOtherPackage<AudioClip>(path, "audio");
            callbackCreate(request);
        }

        public void OnWillPlay(AudioSource audio)
        {
            shaco.Log.Info($"TestAudio OnWillPlay: {_path}");
        }

        public void OnWillResume(AudioSource audio)
        {
            shaco.Log.Info($"TestAudio OnWillResume: {_path}");
        }

        public void OnWillPause(AudioSource audio)
        {
            shaco.Log.Info($"TestAudio OnWillPause: {_path}");
        }

        public void OnWillStop(AudioSource audio)
        {
            shaco.Log.Info($"TestAudio OnWillStop: {_path}");
        }

        public void OnWillAutoStop(AudioSource audio)
        {
            shaco.Log.Info($"TestAudio OnWillAutoStop: {_path}");
        }
    }

    public Button buttonStart;

    public Button buttonSettings;

    public Button buttonFrameworkTest;

    public Text textUIOpenProgress;

    public void OnUIAnimationBreak()
    {
    }

    public IEnumerator RunUICloseAnimation()
    {
        shaco.GameHelper.sound.Play(new TestAudio($"{ShootMan.GlobalDefines.HOTUPDATE_ROOT}Audio/ui_hide.wav"));
        this.gameObject.SetActive(false);
        yield break;
    }

    public IEnumerator RunUIOpenAnimation()
    {
        shaco.GameHelper.sound.Play(new TestAudio($"{ShootMan.GlobalDefines.HOTUPDATE_ROOT}Audio/ui_show.wav"));
        this.transform.localScale = Vector3.zero;
        yield return this.gameObject.ScaleBy(Vector3.one, 1f);
        this.gameObject.SetActive(true);
    }

    void OnUIOpen(shaco.Base.BaseEventArg arg)
    {
        shaco.Log.Info("MainLayer c# open");

        this.buttonStart.onClick.AddListener(this.OnClickStart);
        this.buttonSettings.onClick.AddListener(this.OnClickSettings);
        this.buttonFrameworkTest.onClick.AddListener(this.OnClickOpenShaocFrameworkTestScene);

        //开启新手引导
        var newguideSettingAsset = shaco.GameHelper.res.Load<TextAsset>($"{ShootMan.GlobalDefines.HOTUPDATE_ROOT}NewGuide/GuideSetting.bytes");
        shaco.GameHelper.newguide.LoadFromString((newguideSettingAsset.assetObject as TextAsset).text);
        shaco.GameHelper.newguide.Start();
        shaco.GameHelper.res.Release(newguideSettingAsset);

        //监听ui打开进度事件
        this.AddAutoReleaseEvent<shaco.UIStateChangedEvents.OpenAysncUIProgressEvent>((sender, arg) =>
        {
            this.textUIOpenProgress.text = arg.uiKey + " " + arg.percent;
            shaco.Log.Info("load ui progress=" + this.textUIOpenProgress.text);
        });
    }

    void OnUIClose()
    {
        this.buttonStart.onClick.RemoveListener(this.OnClickStart);
        this.buttonSettings.onClick.RemoveListener(this.OnClickSettings);
        this.buttonFrameworkTest.onClick.RemoveListener(this.OnClickOpenShaocFrameworkTestScene);
    }

    void OnClickStart()
    {
        StartCoroutine(DelayStart());
    }

    void OnClickSettings()
    {
        shaco.GameHelper.ui.OpenUI<SettingLayer>();
    }

    void OnClickOpenShaocFrameworkTestScene()
    {
        shaco.SceneManager.LoadScene("TestMainMenu");
    }

    IEnumerator DelayStart()
    {
        var loadObj = shaco.GameHelper.res.LoadEx<GameObject>($"{ShootMan.GlobalDefines.HOTUPDATE_ROOT}Prefabs/UI/GameRoot.prefab");
        MonoBehaviour.Instantiate(loadObj);
        yield return null;
        this.CloseMe();
    }
}